package com.erdos.core.util.service.impl;

import java.io.File;
import java.util.List;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import com.erdos.core.util.service.MailService;


public class MailServiceImpl implements MailService {

	private Log logger = LogFactory.getLog(MailServiceImpl.class);

	private JavaMailSender mailSender;

	public JavaMailSender getMailSender() {
		return mailSender;
	}

	public void setMailSender(JavaMailSender mailSender) {
		this.mailSender = mailSender;
	}

	public void doTask() throws Throwable {
		System.out.println("$$$$$$$$$$$$$$$$$$do my job");
	}

	/**
	 * 以文本方式发送邮件
	 * 
	 * @param toRecipients
	 *            收件人列表
	 * @param ccRecipients
	 *            抄送人列表
	 * @param subject
	 *            主题
	 * @param message
	 *            邮件內容
	 * @param from
	 *            发送人
	 * 
	 */
	public void sendMail(List<String> toRecipients, List<String> ccRecipients,
			String subject, String message, String from) {
		// 建立邮件讯息
		SimpleMailMessage mailMessage = new SimpleMailMessage();
		// 设定收件人、抄送人、寄件人、主题与内文
		if (toRecipients != null && toRecipients.size() > 0) {
			mailMessage.setTo(toRecipients.toArray(new String[toRecipients
					.size()]));
		}
		if (ccRecipients != null && ccRecipients.size() > 0) {
			mailMessage.setCc(ccRecipients.toArray(new String[ccRecipients
					.size()]));
		}
		mailMessage.setFrom(from);
		mailMessage.setSubject(subject);
		mailMessage.setText(message);
		// mailMessage.
		// 传送邮件
		mailSender.send(mailMessage);
	}

	/**
	 * 以HTML方式发送邮件
	 * 
	 * @param toRecipients
	 *            收件人列表
	 * @param ccRecipients
	 *            抄送人列表
	 * @param subject
	 *            主题
	 * @param message
	 *            邮件内容
	 * @param from
	 *            发送人
	 * 
	 */
	public void sendMailHtml(List<String> toRecipients,
			List<String> ccRecipients, String subject, String message,
			String from) {
		// 建立邮件讯息
		MimeMessage mailMessage = mailSender.createMimeMessage();
		MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage);

		// 设定收件人、寄件人、主题与内文

		try {
			messageHelper.setTo(toRecipients.toArray(new String[toRecipients
					.size()]));
			messageHelper.setCc(ccRecipients.toArray(new String[ccRecipients
					.size()]));
			messageHelper.setFrom(from);
			messageHelper.setSubject(subject);
			messageHelper.setText(message, true);
			// messageHelper.
		} catch (MessagingException e) {
			logger.error(e);
		}
		// 传送邮件
		mailSender.send(mailMessage);
	}

	/**
	 * 以文本方式发送有附件的邮件
	 * 
	 * @param toRecipients
	 *            收件人列表
	 * @param ccRecipients
	 *            抄送人列表
	 * @param subject
	 *            主题
	 * @param message
	 *            邮件內容
	 * @param from
	 *            发送人
	 * @param file
	 *            附件
	 * 
	 */
	public void sendMail(List<String> toRecipients, List<String> ccRecipients,
			String subject, String message, String from, File file) {
		// 建立邮件讯息
		MimeMessage mailMessage = mailSender.createMimeMessage();
		MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage);

		// 设定收件人、寄件人、主题、内文、附件

		try {
			messageHelper.setTo(toRecipients.toArray(new String[toRecipients
					.size()]));
			messageHelper.setCc(ccRecipients.toArray(new String[ccRecipients
					.size()]));
			messageHelper.setFrom(from);
			messageHelper.setSubject(subject);
			messageHelper.setText(message);
			if (file != null) {
				FileSystemResource fileSystemResource = new FileSystemResource(
						file);
				messageHelper.addAttachment(file.getName(), fileSystemResource);
			}
		} catch (MessagingException e) {
			logger.error(e);
		}
		// 传送邮件
		mailSender.send(mailMessage);

	}

	/**
	 * 以HTML方式发送有附件的邮件
	 * 
	 * @param toRecipients
	 *            收件人列表
	 * @param ccRecipients
	 *            抄送人列表
	 * @param subject
	 *            主题
	 * @param message
	 *            邮件內容
	 * @param from
	 *            发送人
	 * @param file
	 *            附件
	 * 
	 */
	public void sendMailHtml(List<String> toRecipients,
			List<String> ccRecipients, String subject, String message,
			String from, File file) {
		// 建立邮件讯息
		MimeMessage mailMessage = mailSender.createMimeMessage();
		MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage);

		// 设定收件人、寄件人、主题、内文、附件

		try {
			messageHelper.setTo(toRecipients.toArray(new String[toRecipients
					.size()]));
			messageHelper.setCc(ccRecipients.toArray(new String[ccRecipients
					.size()]));
			messageHelper.setFrom(from);
			messageHelper.setSubject(subject);
			messageHelper.setText(message, true);
			if (file != null) {
				FileSystemResource fileSystemResource = new FileSystemResource(
						file);
				messageHelper.addAttachment(file.getName(), fileSystemResource);
			}
			// messageHelper.
		} catch (MessagingException e) {
			logger.error(e);
		}
		// 传送邮件
		mailSender.send(mailMessage);

	}
}
